﻿using NoMiddleman.Core.Domain;

namespace NoMiddleman.Facades.DataTransferObjects
{
    public class RealEstateDTO
    {
        private bool isHouse;
        public bool IsHouse
        {
            get { return isHouse; }
            set
            {
                if (value)
                {
                    isHouse = true;
                    isPlot = isFlat = false;
                }
            }
        }

        private bool isFlat;
        public bool IsFlat
        {
            get { return isFlat; }
            set
            {
                if (value)
                {
                    isFlat = true;
                    isPlot = isHouse = false;
                }
            }
        }

        private bool isPlot;
        public bool IsPlot
        {
            get { return isPlot; }
            set
            {
                if (value)
                {
                    isPlot = true;
                    isHouse = isFlat = false;
                }
            }
        }


        public int Id { get; set; }
        public AddressDTO Address { get; set; }
        public decimal Surface { get; set; }
        public string Description { get; set; }

        #region Dwelling Properties
        public int RoomCount { get; set; }
        public bool IsRaw { get; set; }
        public bool IsNew { get; set; }
        #endregion

        #region House Properties
        public int FloorCount { get; set; }
        #endregion

        #region Flat Properties
        public int FloorNumber { get; set; }
        public bool HasElevator { get; set; }
        #endregion

        #region Plot Properties
        public bool IsArmed { get; set; }
        public bool IsNonarable { get; set; }
        public bool IsAgricultural { get; set; }
        #endregion
    }

    public class RealEstateFactory
    {
        public static RealEstate GetRealEstate(RealEstateDTO entity)
        {
            if (entity.IsFlat)
            {
                Flat bussinessEntity = new Flat { FloorNumber = entity.FloorNumber, HasElevator = entity.HasElevator };
                FillDwelling(bussinessEntity, entity);
                FillRealEstate(bussinessEntity, entity);
                return bussinessEntity;
            }
            if (entity.IsHouse)
            {
                House bussinessEntity = new House { FloorCount = entity.FloorCount };
                FillDwelling(bussinessEntity, entity);
                FillRealEstate(bussinessEntity, entity);
                Plot plot = new Plot
                                {
                                    Address = bussinessEntity.Address,
                                    Description = bussinessEntity.Description,
                                    PlotType = PlotType.DzialkaBudowlana,
                                    Surface = bussinessEntity.Surface
                                };
                bussinessEntity.Plot = plot;
                return bussinessEntity;
            }
            else
            {
                Plot bussinessEntity = new Plot();
                if (entity.IsNonarable) bussinessEntity.PlotType = PlotType.Nieuzytek;
                else if (entity.IsArmed) bussinessEntity.PlotType = PlotType.DzialkaBudowlana;
                else if (entity.IsAgricultural) bussinessEntity.PlotType = PlotType.Dzialkarolna;
                FillRealEstate(bussinessEntity, entity);
                return bussinessEntity;
            }
        }

        private static void FillRealEstate(RealEstate bussinessEntity, RealEstateDTO entity)
        {
            bussinessEntity.Surface = entity.Surface;
            bussinessEntity.Description = entity.Description;
            bussinessEntity.Address = AddressFactory.GetAddress(entity.Address);
        }

        private static void FillDwelling(Dwelling bussinessEntity, RealEstateDTO entity)
        {
            bussinessEntity.RoomCount = entity.RoomCount;
            bussinessEntity.IsRaw = entity.IsRaw;
            bussinessEntity.IsNew = entity.IsNew;
        }

        public static RealEstateDTO GetRealEstateDTO(RealEstate entity)
        {
            RealEstateDTO r = new RealEstateDTO
                                  {
                                      Address = AddressFactory.GetAddressDTO(entity.Address),
                                      Description = entity.Description,
                                      Id = entity.Id,
                                      Surface = entity.Surface
                                  };
            Dwelling dwelling = entity as Dwelling;
            if (dwelling != null)
            {
                r.IsNew = dwelling.IsNew;
                r.IsRaw = dwelling.IsRaw;
                r.RoomCount = dwelling.RoomCount;
                if (entity is Flat)
                {
                    Flat f = entity as Flat;
                    r.FloorNumber = f.FloorNumber;
                    r.HasElevator = f.HasElevator;
                    r.IsFlat = true;
                }
                else if (entity is House)
                {
                    House h = entity as House;
                    r.FloorCount = h.FloorCount;
                    r.IsArmed = true;
                }
            }
            else
            {
                Plot p = entity as Plot;
                if (p != null)
                    switch (p.PlotType)
                    {
                        case PlotType.DzialkaBudowlana:
                            r.IsArmed = true;
                            break;
                        case PlotType.Dzialkarolna:
                            r.IsAgricultural = true;
                            break;
                        case PlotType.Nieuzytek:
                            r.IsNonarable = true;
                            break;
                    }
            }
            return r;
        }
    }
}